package com.btex.app;

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;

import com.btex.app.entity.BannerBean;
import com.google.gson.Gson;
import com.lidroid.xutils.HttpUtils;
import com.lidroid.xutils.exception.HttpException;
import com.lidroid.xutils.http.RequestParams;
import com.lidroid.xutils.http.ResponseInfo;
import com.lidroid.xutils.http.callback.RequestCallBack;
import com.lidroid.xutils.http.client.HttpRequest;

import org.apache.http.client.CookieStore;
import org.apache.http.cookie.Cookie;
import org.apache.http.impl.client.DefaultHttpClient;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.List;

public class TestActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_test);
        getBannerInfo();
    }


    private void getBannerInfo() {

        final HttpUtils http = new HttpUtils();
        RequestParams params = new RequestParams();

        params.addQueryStringParameter("position", "index_banner");
        int num = (int) ((Math.random() * 9 + 1) * 100000);
        http.send(HttpRequest.HttpMethod.GET, Config.URL_ADVERT_ONE + "?rnd=" + String.valueOf(num), params, new RequestCallBack<String>() {
            @Override
            public void onSuccess(ResponseInfo<String> responseInfo) {
                DefaultHttpClient defaultHttpClient = (DefaultHttpClient) http.getHttpClient();

                CookieStore cookieStore = defaultHttpClient.getCookieStore();


                List<Cookie> cookies = cookieStore.getCookies();
                for (Cookie cookie : cookies) {
                    String name = cookie.getName();
                    if ("SESSID".equals(name)) {
                        String value = cookie.getValue();

                        Log.i("test1234567654321", value);
                    }


                }

                String result = responseInfo.result.toString();
                Log.i("1223333212121212", result);
                try {
                    JSONObject jsonObject = new JSONObject(result);

                    String data = jsonObject.getString(Config.KEY_CODE);

                    if (data.equals("1")) {

                        Gson gson = new Gson();
                        BannerBean bannerBean = gson.fromJson(result, BannerBean.class);


                    } else {


                    }


                } catch (JSONException e) {
                    e.printStackTrace();

                }

            }

            @Override
            public void onFailure(HttpException error, String msg) {

            }
        });
    }


}
